<?php
/**
 * 后台用户模型类
 */

namespace Apps\Acp\Models;

use Apps\Common\Model\BaseModel;

/**
 * Class Goods
 * @package Apps\Acp\Models
 */
class User extends BaseModel
{
    // 定义此model访问的数据库名
    protected static $_DBNAME = 'yyx_fanli';

    // 定义此model访问的表名
    protected static $_TABLENAME = 'user';

    public function getListByConds($conditions, $offset, $limit)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT * FROM ' . self::$_TABLENAME . $where . ' ORDER BY  id DESC ' . $this->assembleLimit($offset, $limit);

        return $this->select($sql, $values);
    }

    public function countListByConds($conditions)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT COUNT(*) AS num FROM ' . self::$_TABLENAME . $where;

        return $this->selectCount($sql, $values);
    }

    public function updateCount($filed, $id, $num)
    {
        $sql = 'UPDATE ' . self::$_TABLENAME . ' SET ' . $filed . '=' . $filed . '+? WHERE id=?';

        return $this->execute($sql, [$num, $id]);
    }

    public function updateTradeCount($filed, $id, $num)
    {
        $sql = 'UPDATE ' . self::$_TABLENAME . ' SET trade_num=trade_num +1 ,' . $filed . '=' . $filed . '+? WHERE id=?';

        return $this->execute($sql, [$num, $id]);
    }

    public function getViews($id)
    {
        $sql = 'SELECT views AS field FROM ' . self::$_TABLENAME . ' WHERE id =?';

        return $this->getOneField($sql, [$id]);
    }

    public function hexiao($uid, $card_num, $day_num)
    {
        $sql = 'UPDATE ' . self::$_TABLENAME . ' SET card_num=card_num+? , plus_num=plus_num +? WHERE id=? AND (buy_num-card_num) >= ? AND (award-plus_num) >= ?';

        return $this->execute($sql, [$card_num, $day_num, $uid, $card_num, $day_num]);
    }
}
